package com.yyw.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 解决跨域设置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration cors = new CorsConfiguration();
        cors.setAllowCredentials(true);  // 是否允许携带Token进行跨域
        cors.addAllowedHeader("*");  // 允许所有的请求头进行跨域
        cors.addAllowedMethod("*");  // 允许所有的请求方式进行跨域
        cors.addAllowedOrigin("*"); // 允许所有来源进行跨域
        source.registerCorsConfiguration("/**",cors);
        return new CorsWebFilter(source);
    }
}
